package com.my.Config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
////开启aop的注解声明式事务
@EnableTransactionManagement
@MapperScan(basePackages = "com.my.mapper")
@ComponentScan(value = "com.my",excludeFilters ={
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
} )
public class RootConfig {
 //这个是mybatis的Mapper文件的位置
  private   String[] mapperLocations=new String[]{"classpath:mybatis/mapper/*.xml"};

 //事务配置
  @Bean
  public PlatformTransactionManager platformTransactionManager(DataSource dataSource){

    return new DataSourceTransactionManager(dataSource);
  }
  //配置数据源
  @Bean
  public DataSource dataSource() throws Exception {

    ComboPooledDataSource dataSource=new ComboPooledDataSource();
    dataSource.setJdbcUrl("jdbc:mysql:///javatest?useUnicode=true&characterEncoding=utf-8&useSSL=true");
    dataSource.setUser("root");
    dataSource.setPassword("123456");
    dataSource.setDriverClass("com.mysql.jdbc.Driver");

    return dataSource;
  }

  //mybatis配置
  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

    sqlSessionFactoryBean.setDataSource(dataSource);
    //扫描
   sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
    return sqlSessionFactoryBean;
  }
        //此代码出自springboot,我不会写所以粘了过来
      public Resource[] resolveMapperLocations() {


    ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    List<Resource> resources = new ArrayList<Resource>();
    if (mapperLocations != null) {
      for (String mapperLocation : mapperLocations) {
        try {
          Resource[] mappers = resourceResolver.getResources(mapperLocation);
          resources.addAll(Arrays.asList(mappers));
        } catch (IOException e) {
          // ignore
        }
      }
    }
    return resources.toArray(new Resource[resources.size()]);
  }


}
